package com.kudian.restful.controller;

import com.kudian.common.persistence.ResultObj;
import com.kudian.common.utils.JsonUtils;
import com.kudian.restful.service.UserBonusService;
import com.kudian.restful.vo.userbonus.*;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2016/7/6.
 */
@RestController
@RequestMapping("userbonus")
public class UserBonusController {
    private static final Logger logger = Logger.getLogger(UserBonusController.class);

    @Autowired
    private UserBonusService userBonusService;

    @RequestMapping(value = "queryBonustypes", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询某一店铺提供的优惠卷", httpMethod = "POST", notes = "查询某一店铺提供的优惠卷", response = QueryBonusTypeRetVO.class)
    public QueryBonusTypeRetVO queryBonustypes(@RequestBody QueryBonusTypeVO vo) {
        QueryBonusTypeRetVO o = new QueryBonusTypeRetVO();
        logger.info("queryBonustypes=>" + JsonUtils.toJson(vo));
        try {
            o.setItems(userBonusService.queryBonustypes(vo));
            o.setErrcode(0);
            o.setErrmsg("查询某一店铺提供的优惠卷成功");
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("查询某一店铺提供的优惠卷失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "receive", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "用户领取优惠卷", httpMethod = "POST", notes = "用户领取优惠卷", response = ResultObj.class)
    public ResultObj receive(@RequestBody ReceiveVO vo) {
        ResultObj o = new ResultObj();
        logger.info("receive=>" + JsonUtils.toJson(vo));
        try {
            o = userBonusService.receive(vo);
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("用户领取优惠卷失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }

    @RequestMapping(value = "queryByclient", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "用户查询自己优惠卷", httpMethod = "POST", notes = "用户查询自己优惠卷", response = QueryBonusRetVO.class)
    public QueryBonusRetVO queryByclient(@RequestBody QueryBonusByClient vo) {
        QueryBonusRetVO o = new QueryBonusRetVO();
        logger.info("queryByclient=>" + JsonUtils.toJson(vo));
        try {
            o = userBonusService.queryByclient(vo);
        } catch (Exception e) {
            // 失败
            o.setErrcode(9999);
            o.setErrmsg("用户查询自己优惠卷失败!");
            e.printStackTrace();
            logger.error(e);
        }
        return o;
    }
}
